#!/usr/bin/env python
# encoding: utf-8
"""
input.py

Created by Morgan Quirk on 2007-09-24.
"""
import pygame

class InputHandler:
    def __init__(self, net_client, player_ent):
        self.net_client = net_client
        self.player_ent = player_ent
        
        self.bindings={
            "+up":self.move((0,-1)),
            "-up":self.move((0,-1)),
            "+down":self.move((0,1)),
            "-down":self.move((0,1)),
            "+left":self.move((-1,0)),
            "-left":self.move((-1,0)),
            "+right":self.move((1,0)),
            "-right":self.move((1,0)), 
        }
        
    def load_config(self, config_file):
        f=open(config_file, "r")
        self.bindings=f.read().replace("\x0d","")
        f.close()
        
    def update(self):
        for event in pygame.event.get(pygame.KEYDOWN):
            bindname="+"+pygame.key.name(event.key)
            if bindname in self.bindings:
                for action in self.bindings[bindname]:
                    action[0](*action[1])
                
    def move(self, direction):
        x,y=direction
        self.player_ent.move( (direction) )
        self.net_ent.send_action("move", *direction)
        #call a move function in the netcode
    
                